package com.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.AxisType;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.Line;

@Controller
@RequestMapping("/echartsController")
public class EchartsController {
	private static final Logger logger = Logger.getLogger(EchartsController.class);
	
	/**
	 *  创建工单页面跳转
	 * 
	 *  @return
	 */
	@RequestMapping(params = "toEchartsDemo")
	public String createWoR(HttpServletRequest request) {
		return "com/buss/echartsDemo";
	}
	
	/**
	 * echarts曲线展示
	 * 
	 * @param request
	 * @return 
	 */
	@RequestMapping(params = "lineDemo")
	@ResponseBody
	public AjaxJson lineDemo(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
	
		j.setSuccess(true);
		j.setMsg(test2().toString());
		 
		return j;
	}
	
	
	/**
	 * echarts柱状展示展示
	 * 
	 * @param request
	 * @return 
	 */
	@RequestMapping(params = "barDemo")
	@ResponseBody
	public AjaxJson barDemo(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
	
		j.setSuccess(true);
		j.setMsg(test3().toString());
		 
		return j;
	}
	
	public GsonOption  test() {
	    //地址:http://echarts.baidu.com/doc/example/line5.html
		GsonOption option = new GsonOption();
	    option.legend("高度(km)与气温(°C)变化关系");

	    option.toolbox().show(true).feature(Tool.mark, Tool.dataView, new MagicType(Magic.line, Magic.bar), Tool.restore, Tool.saveAsImage);

	    option.calculable(true);
	    option.tooltip().trigger(Trigger.axis).formatter("Temperature : <br/>{b}km : {c}°C");

	    ValueAxis valueAxis = new ValueAxis();
	    valueAxis.axisLabel().formatter("{value} °C");
	    option.xAxis(valueAxis);

	    CategoryAxis categoryAxis = new CategoryAxis();
	    categoryAxis.axisLine().onZero(false);
	    categoryAxis.axisLabel().formatter("{value} km");
	    categoryAxis.boundaryGap(false);
	    categoryAxis.data(0, 10, 20, 30, 40, 50, 60, 70, 80);
	    option.yAxis(categoryAxis);

	    Line line = new Line();
	    line.smooth(true).name("高度(km)与气温(°C)变化关系").data(15, -50, -56.5, -46.5, -22.1, -2.5, -27.7, -55.7, -76.5).itemStyle().normal().lineStyle().shadowColor("rgba(0,0,0,0.4)");
	    option.series(line);
	    
	    return option;
	}
	
	/**
	 * 双轴曲线
	 * @return
	 */
	public GsonOption  test2() {
		GsonOption option = new GsonOption();
	    option.title("");
	    option.legend("回收率","欠费");
	    
	    option.toolbox().show(false);
	    option.calculable(true);
	    option.tooltip().trigger(Trigger.axis);
	    
	    //X轴设置
	    ValueAxis valueAxis = new ValueAxis();
	    List<String> data = new ArrayList();
	    data.add("1月1号");
	    data.add("1月2号");
	    data.add("1月3号");
	    data.add("1月4号");
	    data.add("1月5号");
	    valueAxis.setType(AxisType.category);
	    valueAxis.setData(data);
	    option.xAxis(valueAxis);
	    
	    //Y轴设置
	    CategoryAxis hylAxis = new CategoryAxis();
	    hylAxis.setType(AxisType.value);
	    hylAxis.name("回收率(%)");
	    hylAxis.min(0);
	    hylAxis.max(100);
	    hylAxis.interval(10);
	    
	    CategoryAxis qfAxis = new CategoryAxis();
	    qfAxis.setType(AxisType.value);
	    qfAxis.name("欠费(万元)");
	    qfAxis.min(0);
	    qfAxis.max(1000);
	    qfAxis.interval(100);
	    
	    option.yAxis(hylAxis,qfAxis);
	     
	    //Series
	    Line hylLine = new Line();
	    hylLine.name("回收率");
	    hylLine.smooth(true).setType(SeriesType.line);
	    hylLine.data("99.123","94.234","91.879","89.012","97.567");
	    
	    Line qfLine = new Line();
	    qfLine.name("欠费");  
	    qfLine.smooth(true).setType(SeriesType.line);
	    qfLine.data("123","360","87","566","388");  
	    qfLine.yAxisIndex(1);
	    
	    option.series(hylLine,qfLine);
	    
	    return option;
	}
	
	/**
	 * 柱状图
	 * @return
	 */
	public GsonOption  test3() {
		GsonOption option = new GsonOption();
	    option.title("当月巡视情况");
	    option.legend("计划巡视数","当月巡视数");
	    
	    option.toolbox().show(false);
	    option.calculable(true);
	    option.tooltip().trigger(Trigger.axis);
	    
	    //X轴设置
	    ValueAxis valueAxis = new ValueAxis();
	    List<String> data = new ArrayList();
	    data.add("叶宁");
	    data.add("任少平");
	    data.add("邢晓帆");
	    data.add("陆柳敏");
	    data.add("徐俊");
	    valueAxis.setType(AxisType.category);
	    valueAxis.setData(data);
	    option.xAxis(valueAxis);
	    
	    //Y轴设置
	    CategoryAxis yAxis = new CategoryAxis();
	    yAxis.setType(AxisType.value);
	    
	    option.yAxis(yAxis);
	    
	    //Series
	    Bar jhBar = new Bar();
	    jhBar.name("计划巡视数");
	    jhBar.setType(SeriesType.bar);
	    jhBar.data("80","94","91","89","97");
	    
	    Bar sjBar = new Bar();
	    sjBar.name("当月巡视数");
	    sjBar.setType(SeriesType.bar);
	    sjBar.data("80","97","87","86","92");
	    
	    option.series(jhBar,sjBar);
	    return option;
	}
	
	public static void main(String[] args){
		EchartsController c = new EchartsController();
		GsonOption option = c.test3();
		System.out.println(option.toString());
		
	}
}
